// pages/red-packet/red-packet.js
var app = getApp();
Page({
  /**
    * 页面的初始数据
    */
  data: {
    ruleList: [],//规则列表
    isResultMaskShow: 0,//控制规则弹层的显示
    isReceiveRedPacketShow: 0,// 领取成功谈层
    winWidth: 320,//手机屏幕的宽度
    winHeight: 504,//手机屏幕的高度
    imgUrls: [
      {
        "img": "../../images/red-packet/user.jpg", "desc_time": "6秒前提现", 'content': "现金66.6元"
      }, {
        "img": "../../images/red-packet/user.jpg", "desc_time": "6秒前提现", 'content': "现金66.6元"
      }, {
        "img": "../../images/red-packet/user.jpg", "desc_time": "6秒前提现", 'content': "现金66.6元"
      }
    ],//存储轮播假数据
    title: "",//提示表示
    content: '',//红包内容
    playRobots: [],
    btnIsOk: 0,//数据加载过程中禁用BUTTON
    successTips: '恭喜你获得',//领取成功弹层 表示
    successEnve: '0.00',//领取成功弹层显示金额
    successLeftTips: '还需要邀请',//领取成功弹层显示需要人数的提示内容
    successEnveNum: '0',//领取成功需要多少个人
    successRightTips: '个好友领取才可以拆开哦',//领取成功弹层显示需要人数的提示内容
    shareUserList: [],//邀请用户列表
    head_img: '',//当前用户登录头像
    enve_id: '',//当前红包ID
    uid: "",//用户id
    isUpdateShare: 0,//是否执行修改分享状态接口 0 表示不执行
    optionUid: '',//跳转获取到的UID
    optionEnveId: ''//跳转获取到的enve_id
  },
  ruleBtn: function () {
    var _this = this;
    _this.setData({
      isResultMaskShow: 1
    });
  },
  closeRuleMask: function () {
    var _this = this;
    _this.setData({
      isResultMaskShow: 0
    });
  },
  closeBlank: function (e) {
    var _this = this;
    _this.setData({
      isReceiveRedPacketShow: 0
    });
  },
  // 获取活动规则数据
  getRuleData: function () {
    var _this = this;
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
    };
    app.post('/Enve/rule', requestData, function (res) {
      if(res.code==2000){
      var theRuleList = res.data.list;
      _this.setData({
        ruleList: theRuleList
      })
      }else{
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    var scene = decodeURIComponent(options.scene);
    if (scene && scene !== 'undefined') {
      console.log('我是从扫描二维码进来的');
      var sceneParams = new Array();
      sceneParams = scene.split('#');
      console.log(sceneParams);
      var uid = sceneParams[0];
      var enve_id = sceneParams[1];
      _this.setData({
        optionUid: uid,
        optionEnveId: enve_id,
        isUpdateShare: 1
      });
    } else if (options.uid && options.uid !== 'undefined') {
      _this.setData({
        optionUid: options.uid,
        optionEnveId: options.enve_id,
        isUpdateShare: 1
      });
    }
    _this.loop();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this;
    wx.getSystemInfo({
      success: function (res) {
        var theWinHeight = res.windowHeight;
        var theWinWidth = res.windowWidth;
        _this.setData({
          winHeight: theWinHeight,
          winWidth: theWinWidth
        });
      }
    });

  },
  /**
   * 获取token 并请求加载数据
   */
  loop: function () {
    var _this = this;
    var theToken = wx.getStorageSync("token");
    if (!theToken) {
      setTimeout(function () { _this.loop() }, 100);
    } else {
      _this.setData({
        token: theToken
      });
      _this.loadConfig();
      _this.getRuleData();
    }
  },
  /**
   * 加载初始化红包配置文件
   */
  loadConfig: function () {
    var _this = this;
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
    };
    app.post('/Enve/enveConfig', requestData, function (res) {
      if (res.code == 2000) {
        console.log(res);
        _this.setData({
          title: res.data.title,
          content: res.data.tips,
          imgUrls: res.data.robot_list,
          playRobots: res.data.robot_list,
          btnIsOk: 1
        });
        if (res.data.login_user_id == _this.data.optionUid && res.data.enve_id == 0) {
          //包主进入
          wx.redirectTo({
            url: '../withdrawals/withdrawals?enve_id=' + _this.data.optionEnveId,
          })
        } else if (res.data.enve_id > 0) {
          wx.redirectTo({
            url: '../withdrawals/withdrawals?enve_id=' + res.data.enve_id,
          })
        }
      } else if (!res.code) {
        wx.showToast({
          title: '服务器去月球了',
          duration: 2000
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  /**检测机器人数据是否播放完毕 */
  animationfinish: function (e) {
    var _this = this;
    if (_this.data.playRobots.length > 0) {
      var playRobots = _this.data.playRobots;
      playRobots.pop();
      _this.setData({
        playRobots: playRobots
      });
    } else {
      //数据播放完毕重新获取数据
      var requestData = {
        token: _this.data.token,
        version: app.setConfig.version,
      };
      app.post('/Enve/getRobots', requestData, function (res) {
        if (res.code == 2000) {
          _this.setData({
            imgUrls: res.data.robot_list,
            playRobots: res.data.robot_list
          });
        } else if (!res.code) {
          wx.showToast({
            title: '服务器去月球了',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 2000
          });
        }
      });
    }
  },
  /**
   * 领取红包
   */
  saveEnveEvent: function (e) {
    var _this = this;
    _this.setData({ enve_id: '', head_img: '' });
    if (_this.data.btnIsOk == 1) {
      var requestData = {
        token: _this.data.token,
        version: app.setConfig.version,
      };
      app.post('/Enve/createEnve', requestData, function (res) {
        if (res.code == 2000) {
          _this.setData({
            successTips: res.data.enve_success_tips,
            successEnve: res.data.enve_amount,
            successLeftTips: res.data.enve_success_left_tips,
            successEnveNum: res.data.splus_num,
            successRightTips: res.data.enve_success_right_tips,
            shareUserList: res.data.share_user_list,
            isReceiveRedPacketShow: 1,
            head_img: res.data.head_img,
            enve_id: res.data.enve_id,
            uid: res.data.uid,
          });
          //协助他人打开红包
          _this.openEnve();
        } else if (!res.code) {
          wx.showToast({
            title: '服务器去月球了',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 2000
          });
        }
      });
    }
  },
  /*前往邀请好友页面*/
  goShare: function () {
    var _this = this;
    if (_this.data.enve_id && _this.data.head_img) {
      wx.navigateTo({
        url: '../share-red-packet/share-red-packet?enve_id=' + _this.data.enve_id + "&uid=" + _this.data.uid,
      });
    } else {
      wx.showToast({
        title: '无法获取红包信息',
        duration: 2000
      });
    }
  },
  /**
 * 协助他人打开红包
*/
  openEnve: function () {
    var _this = this;
    var isShareEvent = _this.data.isUpdateShare;
    if (isShareEvent == 1) {
      //表示有分享进入协助他人打开红包
      var requestData = {
        token: _this.data.token,
        version: app.setConfig.version,
        enve_id: _this.data.optionEnveId,
        uid: _this.data.optionUid
      };
      app.post('/Enve/openEnve', requestData, function (res) {
        // console.log(res)
        if(res.code!=2000){
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 2000
          });
        }
      });
    }
  },
})